import { window } from 'vscode';
import { global } from '../../global';
import { Fetch, showErrInfo } from '../../utils';

/**
 * 用户
 *
 * @author chitanda
 * @date 2022-12-13 10:12:05
 * @export
 * @class UserService
 */
export class UserService {
  /**
   * 登录
   *
   * @author chitanda
   * @date 2022-12-12 22:12:29
   * @return {*}  {Promise<boolean>}
   */
  async login(): Promise<boolean> {
    const loginname = await window.showInputBox({
      placeHolder: '请输入 iBizSys 平台用户名',
    });
    const password = await window.showInputBox({
      placeHolder: '请输入密码',
      password: true,
    });
    if (loginname && password) {
      try {
        const res = await Fetch.post(`${global.config.cloudAddress}/v7/login`, {
          loginname,
          password,
        });
        const { data } = res;
        if (res.status === 200) {
          await global.setToken(data.token);
          return true;
        }
      } catch (err: unknown) {
        showErrInfo(err);
      }
    }
    return false;
  }

  /**
   * 登出
   *
   * @author chitanda
   * @date 2022-12-12 22:12:24
   * @return {*}  {Promise<boolean>}
   */
  async logout(): Promise<boolean> {
    await global.setToken('');
    return true;
  }
}
